import logging,time
import os
import getpathinfo

path = getpathinfo.get_path()#获取项目根目录
log_path = os.path.join(path,"logs")#存放log的目录
if not os.path.exists(log_path):#log目录不存在则新建
    os.mkdir(log_path)

'''
class Log(object):
    def __init__(self):
        self.log_name = os.path.join(log_path,"%s.log" %time.strftime("%Y_%m_%d"))#log文件名
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.DEBUG)
        self.formatter = logging.Formatter("%(asctime)s - %(levelname)s : %(message)s","%H:%M:%S")

    def __console(self,level,message):
        #创建一个filter写入日志到文件
        fh = logging.FileHandler(self.log_name,"a+",encoding="utf-8")
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(self.formatter)
        self.logger.addHandler(fh)

        #创建一个handler输出到控制台
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(self.formatter)
        self.logger.addHandler(ch)

        if level =="info":
            self.logger.info(message)
        if level =="debug":
            self.logger.debug(message)
        if level =="error":
            self.logger.error(message)
        if level == "warning":
            self.logger.warning(message)

        #防止日志重复
        self.logger.removeHandler(fh)
        self.logger.removeHandler(ch)
        #关闭打开的日志文件
        fh.close()

    def debug(self,message):
        self.__console("debug",message)
    def info(self,message):
        self.__console("info",message)
    def warning(self,message):
        self.__console("warning",message)
    def error(self,message):
        self.__console("error",message)
'''
class Log:
    def __init__(self):
        self.logger = logging.getLogger()
        self.log_name = os.path.join(log_path, "%s.log" % time.strftime("%Y_%m_%d"))  # log文件名
        if not self.logger.handlers:
            self.logger.setLevel(logging.DEBUG)

            # 创建一个handle写入文件
            fh = logging.FileHandler(self.log_name, encoding='utf-8')
            fh.setLevel(logging.INFO)

            # 创建一个handle输出到控制台
            ch = logging.StreamHandler()
            ch.setLevel(logging.INFO)

            # 定义输出的格式
            formatter = logging.Formatter(*self.fmt)
            fh.setFormatter(formatter)
            ch.setFormatter(formatter)

            # 添加到handle
            self.logger.addHandler(fh)
            self.logger.addHandler(ch)

    @property
    def fmt(self):
        return '%(levelname)s %(asctime)s [%(filename)s:%(lineno)d] %(message)s',"%Y-%m-%d %H:%M:%S"
        #return '%(levelname)s %(asctime)s [%(filename)s:%(lineno)d] %(message)s'

log = Log().logger

if __name__ == "__main__":
    json = {"_data":'{"EMPLOYEE_NO":"290135","ACCT_ID":"202112","DAY_OR_MONTH":"month"}',
     "signature":"QUdzlUMISU4MJX+RL1ggig=="}
    log.info(json)









